package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.CustomerMapper;
import com.gxa.insurance.pojo.Customer;
import com.gxa.insurance.service.CustomerService;
import com.gxa.insurance.util.StringUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CustomerServiceImpl
 * @Description TODO
 * @Author ds
 * @Date 2022/6/7 13:08
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = {Throwable.class})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    @Override
    public void add(Customer customer) throws Exception{
        customer.setLoginTime(new Date(System.currentTimeMillis()));
        customerMapper.insert(customer);
    }

    @Override
    public Map<String,Object> selectByPage(Integer page, Integer size) throws Exception {

        Map<String,Object> map = new HashMap();
        Integer count = customerMapper.selectAllCount();
        List<Customer> customers = customerMapper.selectByPage((page-1)*size,size);
        System.out.println(customers);
        if(customers.size() == 0){
            throw new BusinessException(ExceptionCode.CUSTOMER_IS_EMPTY);
        }
        map.put("count",count);
        map.put("customers",customers);
        return map;
    }


    @Override
    public void update(Customer customer) throws Exception {
        customer.setLoginTime(new Date(System.currentTimeMillis()));
        customerMapper.update(customer);
    }

    @Override
    public Customer selectById(Integer id) throws Exception {
        Customer customer = customerMapper.selectById(id);
        if (customer == null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        return customer;
    }

    /**
     * 前台根据手机号查询用户
     * @param phone
     * @return
     * @throws Exception
     */
    @Override
    public Customer selectByPhone(String phone) throws Exception {
        Customer customer = customerMapper.selectByPhone(phone);
        if (customer == null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        return customer;
    }

    @Override
    public Map<String,Object> selectLikeName(Map map) throws BusinessException {

        List<Customer> customers = null;
        Map<String,Object> Listmap = new HashMap();

        Object page = map.get("page");
        Object size = map.get("size");
        int intPage = Integer.parseInt(String.valueOf(page));
        int intPageSize = Integer.parseInt(String.valueOf(size));
        intPage=(intPage-1)*intPageSize;
        map.put("page",intPage);
        map.put("size",intPageSize);

        if(StringUtil.isEmpty((String)map.get("name"))){
            customers = customerMapper.selectByPage(intPage, intPageSize);
        }
        Integer count = customerMapper.selectLikeNameCount(map);

        customers = customerMapper.selectLikeName(map);
        if(customers.size()==0){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        Listmap.put("count",count);
        Listmap.put("customers",customers);
        return Listmap;
    }

    @Override
    public List<Customer> selectEmail() throws Exception {
        List<Customer> customers = customerMapper.selectEmail();
        if(customers.size() == 0){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        return customers;
    }

    @Override
    public boolean selectByEmail(String email) throws Exception {
        List<Customer> customers = customerMapper.selectByEmail(email);
        if (customers.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }

    @Override
    public Map<String,Object> selectByEmailAndCoupon(String email) throws Exception {
        Map<String,Object> map = new HashMap();
        Customer customer = customerMapper.selectByEmailAndCoupon(email);

        if(customer==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        map.put("customerId",customer.getId());
        map.put("isBoolean",true);
        return map;
    }


    /**
     * 前台登录判断
     * @param phone
     * @return
     */
    @Override
    public Map<String,Object> Register(String phone) {
        Map<String,Object> map = new HashMap();
        Customer customer = customerMapper.selectByPhone(phone);
        if(customer!=null){
            map.put("isBoolean","login");
            map.put("Customer",customer);
        }else{
            Customer customer1 = new Customer();
            customer1.setPhone(phone);
            customerMapper.insert(customer1);
            map.put("Customer",customer1);
            map.put("isBoolean","register");
        }
        return map;
    }


}
